import html2canvas from "html2canvas"
import JSPDF from "jspdf"
export default {
  install (Vue, options) {
    Vue.prototype.GeneratePdf = function (htmlTitle) {
      var element = document.getElementById("pdfGen")
      html2canvas(element, {
        logging: false,
      allowTaint: true,
      }).then(function (canvas) {
        var pdf = new JSPDF("p", "mm", "a4")
        var ctx = canvas.getContext("2d")
        var a4w = 170; var a4h = 257
        var imgHeight = Math.floor(a4h * canvas.width / a4w)
        var renderedHeight = 0

        while (renderedHeight < canvas.height) {
          var page = document.createElement("canvas")
          page.width = canvas.width
          page.height = Math.min(imgHeight, canvas.height - renderedHeight)

          page.getContext("2d").putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0)
          pdf.addImage(page.toDataURL("image/jpeg", 1.0), "JPEG", 18, 10, a4w, Math.min(a4h, a4w * page.height / page.width))

          renderedHeight += imgHeight
          if (renderedHeight < canvas.height) { pdf.addPage() }
          // delete page;
        }
        pdf.save(htmlTitle)
      })
    }
  }
}
